package com.shengxing.lljl.payment.Alipay.controller;

import cn.hutool.http.ContentType;
import com.shengxing.lljl.common.Result;
import com.shengxing.lljl.common.properties.AlipayProperties;
import com.shengxing.lljl.payment.Alipay.entity.AliPayReq;
import com.shengxing.lljl.payment.Alipay.service.IAlipayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支付控制器
 *
 * @author javgo.cn
 * @date 2024/1/13
 */
@Controller
@Slf4j
@RequestMapping("/lljl/alipay")
public class AlipayController {

    @Resource
    private AlipayProperties alipayConfig;

    @Resource
    private IAlipayService alipayService;

    @ApiOperation("支付宝电脑网站支付")
    @GetMapping("/pcPayment")
    public void pcPayment(AliPayReq aliPayReq, HttpServletResponse response) throws IOException {
        response.setContentType(ContentType.TEXT_HTML.getValue() + ";charset=" + alipayConfig.getCharset());
        Result<?> result = alipayService.initiatePcPayment(aliPayReq);
        response.getWriter().write(result.getData().toString());
        response.getWriter().flush();
        response.getWriter().close();
    }

//    @ApiOperation("支付宝手机网站支付")
//    @GetMapping("/mobilePayment")
//    public void mobilePayment(AliPayReq aliPayReq, HttpServletResponse response) throws IOException {
//        response.setContentType(ContentType.TEXT_HTML.getValue() + ";charset=" + alipayConfig.getCharset());
//        Result<?> result = alipayService.initiateMobilePayment(aliPayReq);
//        response.getWriter().write(result.getData().toString());
//        response.getWriter().flush();
//        response.getWriter().close();
//    }

//    @ApiOperation("支付宝支付通知")
//    @PostMapping("/notify")
//    @ResponseBody
//    public Result<?> processPaymentNotification(HttpServletRequest request) {
//        Map<String, String> params = new HashMap<>();
//        Map<String, String[]> requestParams = request.getParameterMap();
////        requestParams.keySet().forEach(r -> params.put(r, request.getParameter(r)));
//        requestParams.forEach((k, v) -> {
//            params.put(k, Arrays.stream(v).collect(Collectors.joining()));
//        });
////        log.info("params的值：{}", params);
////        log.info("requestParams.keySet():{}",requestParams.keySet());
////        log.info("requestParams.values(): {}",
////                requestParams.values().stream()
////                        .map(Arrays::toString) // 将每个数组转换为字符串
////                        .collect(Collectors.joining(", ")));
////        log.info("trade_status:{}",params.get("trade_status"));
//        params.put("trade_status", TradeStatusType.of(params.get("trade_status"));
//        return alipayService.processPaymentNotification(params);
//    }

    @ApiOperation("支付宝支付通知")
    @PostMapping("/notify")
    @ResponseBody
    public Result<?> processPaymentNotification(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        requestParams.keySet().forEach(r -> params.put(r, request.getParameter(r)));
        return alipayService.processPaymentNotification(params);
    }

    @ApiOperation("查询支付状态")
    @GetMapping("/queryPaymentStatus")
    @ResponseBody
    public Result<?> queryPaymentStatus(String outTradeNo, String tradeNo) {
        return alipayService.queryPaymentStatus(outTradeNo, tradeNo);
    }
}
